package com.xinzhitong.www.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinzhitong.www.exception.SystemMessageException;
import com.xinzhitong.www.exception.systemmessage.EmptySystemMessageException;
import com.xinzhitong.www.mapper.SystemMessageMapper;
import com.xinzhitong.www.pojo.SystemMessage;
import com.xinzhitong.www.pojo.User;
import com.xinzhitong.www.service.MessageService;
import com.xinzhitong.www.utils.FileHelper;
import com.xinzhitong.www.utils.MyPageInfo;
import com.xinzhitong.www.utils.SearchKeyUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 消息服务实现类
 *
 * @author 徐鑫
 */
@Service
@Transactional
public class MessageServiceImpl implements MessageService {

    @Resource
    SystemMessageMapper systemMessageMapper;

    @Override
    public MyPageInfo getSystemMessage(Integer pageNo, Integer pageSize) throws SystemMessageException {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        PageHelper.startPage(pageNo, pageSize);
        List<SystemMessage> systemMessageList = systemMessageMapper.selectSystemMessagesByUsername(user.getUsername());
        return allAddSystemUrl(systemMessageList);
    }

    @Override
    public MyPageInfo searchSystemMessage(Integer pageNo, Integer pageSize, String keyWord) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        PageHelper.startPage(pageNo, pageSize);
        List<SystemMessage> systemMessageList = systemMessageMapper.selectSystemMessagesByUsernameWithConditions(
                user.getUsername(),
                "title like '" + SearchKeyUtil.makeKey(keyWord) + "'"
        );
        return allAddSystemUrl(systemMessageList);
    }

    @Override
    public void removeMessages(Integer[] messageIds) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();

        StringBuilder idRange = new StringBuilder();
        idRange.append("(");
        for (int i = 0; i < messageIds.length; i++) {
            if (i != 0)
                idRange.append(", ");
            idRange.append(messageIds[i]);
        }
        idRange.append(")");

        systemMessageMapper.deleteSystemMessage(user.getUsername(), idRange.toString());
    }

    @Override
    public void ignoreMessage(Integer messageId) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        if (systemMessageMapper.selectTheSystemMessage(user.getUsername(), messageId) == null)
            throw new EmptySystemMessageException();
        systemMessageMapper.updateSystemMessage(
                "has_read",
                1,
                "username='" + user.getUsername() + "' and id=" + messageId);
    }

    @Override
    public SystemMessage[] getNewestSystemMessage(Integer size) {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        SystemMessage[] systemMessages = systemMessageMapper.selectTheNewestMessage(size, user.getUsername());
        if (systemMessages.length == 0)
            throw new EmptySystemMessageException();
        return systemMessages;
    }

    @Override
    public Integer getNumberOfSystemMessages() {
        Subject subject = SecurityUtils.getSubject();
        User user = (User) subject.getPrincipal();
        return systemMessageMapper.countNewSystemMessages(user.getUsername());
    }

    /**
     * 为list中的系统消息对象添加系统url并进行自定义分页
     *
     * @param systemMessageList 系统url
     * @return 处理完成的消息列表
     */
    private MyPageInfo allAddSystemUrl(List<SystemMessage> systemMessageList) {
        for (SystemMessage systemMessage : systemMessageList)
            systemMessage.setUrl(FileHelper.serverURL + systemMessage.getUrl());
        if (systemMessageList.size() == 0)
            throw new EmptySystemMessageException();
        PageInfo<SystemMessage> systemMessagePageInfo = new PageInfo<>(systemMessageList);
        return new MyPageInfo(systemMessagePageInfo);
    }

}
